package com.higold.cloud.mvp.presenter

import com.higold.cloud.base.BasePresenter
import com.higold.cloud.mvp.contract.DevelopListContract
import com.higold.cloud.mvp.model.DevelopListModel
import com.higold.cloud.net.exception.ExceptionHandle
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 *    author : lxz
 *    date   : 2019/9/7 10:44
 *    desc   :
 */
class DevelopListPresenter : BasePresenter<DevelopListContract.View>(), DevelopListContract.Presenter {

    private val developListModel by lazy { DevelopListModel() }

    override fun getUploadPic(file: String) {
        checkViewAttached()
        mRootView?.showLoading()

        val file = File(file)
        val requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file)
        val filePart = MultipartBody.Part.createFormData("file", file.name, requestFile)

        val disposable = developListModel.requestUploadPic(filePart)
                .subscribe({ uploadPic ->
                    mRootView?.setUploadPic(uploadPic)
                }, { throwable ->
                    //处理异常
                    mRootView?.showError(ExceptionHandle.handleException(throwable), ExceptionHandle.errorCode)
                })
        addSubscription(disposable)
    }

    override fun getDevelopList() {
        checkViewAttached()
        mRootView?.showLoading()
        val disposable = developListModel.requestDevelopList()
                .subscribe({ developList ->
                    mRootView?.setDevelopList(developList)
                }, { throwable ->
                    //处理异常
                    mRootView?.showError(ExceptionHandle.handleException(throwable), ExceptionHandle.errorCode)
                })
        addSubscription(disposable)
    }
}